package hotel.meituan.oversea.biz.task;

import com.alibaba.fastjson.JSON;
import hotel.base.oversea.constant.CommonConstants;
import hotel.base.oversea.utils.DateUtil;
import hotel.base.oversea.utils.Result;
import hotel.meituan.oversea.biz.utils.RestTemplateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

/**
 * 处理美团产品-异步通知-产品房态变化 异步线程
 */
@Slf4j
@Component
public class MeituanDealNotifyRpBreakfastChangeTask {
    private String timestamp = "Mdnrgsct"+String.valueOf(System.currentTimeMillis());

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 对于变更的房态数据  推送给携程房态更新接口
     * @throws Exception
     */
    //@Async 标识这个方法异步执行，可返回void或Future
    @Async
    public void requestNoticeSwitchsUpdateRoomChange(Map<String,Integer> changeInfos){
       /* log.info("Meituan Deal Notify Rp Breakfas Change Task {} >>>>>>>>> 异步线程 通知携程房态变更异步线程 MeituanDealNotifyRpGoodsStateChangeTask.requestNoticeSwitchsUpdateRoomChange 开始执行时间：{}",timestamp,DateUtil.getNowTime());

        String accessToken = HttpClientBaseAuth.getAuthToken();
        String switchsHost = CommonUrlConstants.SWITCHS_HOST;
        String updateSwitchChangeRoomUrl = switchsHost + UrlSwitchsEnums.switchRoom.UPDATE_ROOM_CHANGE.getValue();

        for (Map.Entry vo:changeInfos.entrySet()){
            String goodsId = ((String) vo.getKey()).split(",")[0];
            String date = ((String) vo.getKey()).split(",")[1];
            Integer state = (Integer)vo.getValue();
            //携程接口status 0 满房(美团的0，2，3归类于0) 1可预定
            if(0== state || 2 == state || 3 == state){
                state = 0;
            }
            requestSwitchsApi(updateSwitchChangeRoomUrl,Integer.valueOf(goodsId),date,state);
            try{
                Thread.currentThread().sleep(6000);
            }catch (Exception e){
                log.error(e.getMessage(),e);
            }
        }

        log.info("Meituan Deal Notify Rp Breakfas Change Task {} >>>>>>>>> 异步线程 MeituanDealNotifyRpGoodsStateChangeTask.requestSwitchsApi 结束执行时间：{}",timestamp,DateUtil.getNowTime());*/
    }

    public void requestSwitchsApi(String updateSwitchChangeRoomUrl,Integer goodsId,String date,Integer state){
        Map params = new HashMap();
        params.put("mtGoodId",goodsId);
        params.put("status",state);
        params.put("time",date);
        log.info("Meituan Deal Notify Rp Breakfas Change Task {} >>>>>>>>> 异步线程 通知携程房态变更异步线程进入方法 调用携程修改房态接口 " +
                "MeituanDealNotifyRpGoodsStateChangeTask.requestSwitchsApi：{},{}",timestamp, JSON.toJSONString(params),DateUtil.getNowTime());
        Result response = RestTemplateUtils.post(updateSwitchChangeRoomUrl, JSON.toJSONString(params),restTemplate);
        if(response.getCode() == CommonConstants.SUCCESS){

        }else{
            log.info("Meituan Deal Notify Rp Breakfas Change Task {} 通知携程房态变更异步线程进入方法 调用携程修改房态接口  MeituanDealNotifyRpGoodsStateChangeTask.requestSwitchsApi 请求失败：{}",timestamp,response);
        }
    }

   /* @Async
    public Future<String> doTaskTwo() throws Exception {
        System.out.println("task2");
        return new AsyncResult<>("任务一完成");
    }*/
}
